// pages/addIntentionProject/addIntentionProject.js

//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
	  khmcinput:'',
	  khmcList:[],
	  khmcId:[],
	 nfList:[],
	 nfId:'',
	  index:0,
	  xmjbList:[],
	  xmjbId:'',
    index1:0,
	xmlxdlList:[],
	xmlxdlId:'',
	index2:0,
	xmlxxlList:[],
	xmlxxlId:'',
	index3:0,
	sffsList:[],
	sffsId:'',
	index4:0,
	projectName:'',
	
	
	loginName: app.globalData.loginName,
  },
  aKhmc: function (e) {
    console.log(e);
	console.log(e.target.dataset.id);
    this.setData({
      khmcinput: e.target.dataset.text,
      khmcId: e.target.dataset.id,
    })
  },
  // 客户名称模糊搜索
  changeSearch: function (event) {
    console.log(event.detail);
    let that = this;
    var inputSearch = event.detail.value;
    that.getKhmc(inputSearch);
  },
  bindPickerChange: function(e) {
	console.log(e);
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  bindPickerChange1: function (e) {
    console.log(e);
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index1: e.detail.value
    })
  },
  bindPickerChange2: function (e) {

    console.log(e);
    console.log('picker发送选择改变，携带值为', e.detail.value);
	console.log(this.data.xmlxdlList[e.detail.value].id);
	var cid=this.data.xmlxdlList[e.detail.value].id;
    this.setData({
      index2: e.detail.value
    })
	this.getXmlxxl(cid);
	
  },
  bindPickerChange3: function (e) {
	  	  var that=this;
    console.log(e);
    console.log('picker发送选择改变，携带值为', e.detail.value)	
    this.setData({
      index3: e.detail.value,
    })
	var pn=that.data.nfList[that.data.index].name+that.data.xmjbList[that.data.index1].name+that.data.xmlxxlList[that.data.index3].name+'('+that.data.xmlxdlList[that.data.index2].name+')';
	console.log(pn);
	
	that.setData({
	  projectName:pn,
	})
  },
  bindPickerChange4: function (e) {
    console.log(e);
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index4: e.detail.value
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {    
	this.getYxxm();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  formSubmit: function (e) {
    var that = this; 
   

    wx.showToast({
      icon: 'loading',
      title: 'Loading',
      duration: 2000,
      mask: true
    })

    console.log(this.data.clientName);
    console.log('form发生了submit事件，携带数据为：', e.detail.value);
    let { loginName, clientId, year, administrativeCategories,projectName, projectType, projectLittleType, chargingMethods, itemAmount,projectScale,remark} = e.detail.value;
    if (!clientId || !projectLittleType || !chargingMethods || !itemAmount || !projectScale|| !remark) {
      wx.showToast({
        title: '请填写完整信息！',
        icon: 'none',
        duration: 2000
      })
      //wx.hideToast();
      return;
    }
    wx.request({
      url: app.globalData.apiUrl + '/wx/wxaddIntentionProjectDo', //接口地址
      data: e.detail.value,
      success: function (res) {
        console.log("新增客户信息=", res)
       if(res.data.code == 0){         
         wx.showToast({
           title: '操作成功！',
           icon: 'success',
           duration: 2000,
		   success:function(e){
		    setTimeout(function(){
				wx.navigateTo({
				  url: '../app-ywb-home/app-ywb-home',  //提交成功去到你的那个列表页
				})
			},2000)
		   }
         })

       }else{
         wx.showToast({
           title: res.data.msg,
           icon: 'none',
           duration: 2000
         })
       }
        
      }
    })
  },  

  //加载客户名称数据字典
  getKhmc: function (codes) {
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + '/wx/AllClientData', //接口地址
      data: {
		loginName: app.globalData.loginName,
        clientName: codes,
      },
      success: function (res) {
        console.log("数据字典=", res)
        that.setData({
          khmcList: res.data.list,
        })
      }
    })
  },
  //加载项目类型小类
  getXmlxxl: function (codes) {
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + '/wx/wxgetProjectTypeDict', //接口地址
      data: {
  		loginName: app.globalData.loginName,
        id: codes,
      },
      success: function (res) {
        console.log("数据字典=", res)
        that.setData({
          xmlxxlList: res.data.data,
		  xmlxxlId:res.data.data[0].id,
        })
		var pn=that.data.nfList[that.data.index].name+that.data.xmjbList[that.data.index1].name+res.data.data[0].name+'('+that.data.xmlxdlList[that.data.index2].name+')';
		console.log(pn);
		
		that.setData({
		  projectName:pn,
		})
      }
    })
  },
  getYxxm:function(){
	  var that=this;
	  wx.request({
		  url:app.globalData.apiUrl+'/wx/wxaddIntentionProject',
		  data: {
		    loginName: app.globalData.loginName,
			},
		  success:function(res){
			  res.data.data.ProjectType.unshift({name:'请选择'});
			  console.log("数据字典客户名称=",res)
			  that.setData({
				  khmcList:res.data.data.client,
				  nfList:res.data.data.AParticularYear,
				  nfId:res.data.data.AParticularYear[0].id,
				  xmjbList:res.data.data.ProjectLevel,
				  xmjbId:res.data.data.ProjectLevel[0].id,
				  xmlxdlList:res.data.data.ProjectType,
				  sffsList:res.data.data.chargingMethods,
			  })
		  }
	  })
  },
})